package com.gombe.credit.app.configs

import com.gombe.credit.app.BuildConfig
import com.gombe.credit.app.sdk.analyse.GombeCreditAnalyseUtil
import com.gombe.credit.app.utils.GombeCreditDateUtil
import com.gombe.credit.app.utils.shared.GombeCreditSharedHelper
import com.gombe.credit.app.utils.shared.GombeCreditSharedKey
import org.json.JSONObject
import java.util.Random
import java.util.TimeZone

/**
 * ApiUrl
 * @Author wwdeng
 * @CreateTime: 2024/12/5 15:48
 * @Annotation：
 */
object GombeCreditApiUrl {

    /**
     * https://www.zoeinfotech.online
     * http://159.138.165.72
     * 域名
     */
    const val GOMBECREDIT_HOST = "https://www.zoeinfotech.online"

    /**
     * 地区转发
     * /v1/timezonePath
     */
    const val GOMBECREDIT_TIMEZONE_URL = "/pY6oajaF4zPeI7TDPprhA"

    /**
     * APP业务统一请求接口
     * /v1/commonApi
     */
    private val GOMBECREDIT_COMMON_URLS = mutableListOf(
        "/UxHYTlxtyhuqs2etoPwaUQ",
        "/RObljI1kdR2rQzVcbvDypg",
        "/to3PFKaflG5AnbwlG6XZ6w",
        "/Eko51OdY6ObwqFSeQCugw",
    )

    /**
     * 图片上传
     * /v1/imageUpload
     */
    const val GOMBECREDIT_UPLOAD_FILE_URL = "/yEk6ZJBqcYWPFFZAFd6VQ"

    /**
     * 获取时区转发url
     */
    fun takeLaunchTimeZoneUrl(): String{
        return "$GOMBECREDIT_HOST$GOMBECREDIT_TIMEZONE_URL"
    }

    /**
     * 图片上传url
     */
    fun takeUploadFileUrl(): String{
        return takeFormatUrl(GOMBECREDIT_UPLOAD_FILE_URL)
    }

    /**
     * 埋点接口url
     */
    fun takeTrackPointUrl(): String{
        return takeFormatUrl(getRandomUrl(0))
    }


    /**
     * 通用接口url
     */
    fun takeRequestUrl(): String{
        val url = getRandomUrl()
        return takeFormatUrl(url)
    }

    /**
     * 格式化url: host/timezone/path
     */
    private fun takeFormatUrl(url: String): String{
        //地区转发
        val timeZone = GombeCreditSharedHelper.takeGombeCreditSharedString(GombeCreditSharedKey.GOMBECREDIT_TIMEZONE)
        if (timeZone.isNotEmpty()){
            return "$GOMBECREDIT_HOST/$timeZone$url"
        }
        return "$GOMBECREDIT_HOST$url"
    }

    /**
     * 添加公共参数
     */
    fun JSONObject.addCommonParams(): JSONObject {
        apply {
            put("product", GombeCreditConfigs.GOMBECREDIT_APPNAME)                                           //  产品 APP名称
            put("token", GombeCreditSharedHelper.takeGombeCreditSharedString(GombeCreditSharedKey.GOMBECREDIT_TOKEN))                                               //  token
            put("apiVersion","v3")                                                      //  接口版本
            put("androidId", GombeCreditAnalyseUtil.getAndroidId())                                    //  安卓id
            put("advertId", GombeCreditAnalyseUtil.getAdId())                                          //  广告id
            put("aduid", GombeCreditAnalyseUtil.getAdId())                                             //  广告id
            put("aduidPath", GombeCreditAnalyseUtil.getAndroidId())                                    //  广告地址
            put("isLimitAdTrackingEnabled", GombeCreditAnalyseUtil.getAdEnabled().toString())          //  是否限制广告id，1是 0否
            put("userId", GombeCreditSharedHelper.takeGombeCreditSharedString(GombeCreditSharedKey.GOMBECREDIT_USERID))                                             //  userId
            put("version", BuildConfig.VERSION_CODE)                                        //  app版本号
            put("timp", System.currentTimeMillis())                                            //  时间戳
            put("country", GombeCreditSharedHelper.takeGombeCreditSharedString(GombeCreditSharedKey.GOMBECREDIT_TIMEZONE_COUNTRY))                                  //国家标识
            put("tzid", TimeZone.getDefault().id)                                          //当前时区id
            put("localTimestamp","${System.currentTimeMillis()}")                       //时间戳
//            put("path","")                                                                        //  传映射后的接口路径，不带前面的/号
        }
        return this.addCustomParams()
    }

    /**
     * 添加随机参数增量 4
     */
    private fun JSONObject.addCustomParams(): JSONObject {
        val loginMobile = GombeCreditSharedHelper.takeGombeCreditSharedString(GombeCreditSharedKey.GOMBECREDIT_MOBILE)
        val launchTime = GombeCreditSharedHelper.takeGombeCreditSharedString(GombeCreditSharedKey.GOMBECREDIT_APP_START_TIME)
        val loginTime = GombeCreditSharedHelper.takeGombeCreditSharedString(GombeCreditSharedKey.GOMBECREDIT_LOGIN_TIME)
        val currentTime = GombeCreditDateUtil.getFormatTime(System.currentTimeMillis(),"dd/MM/yyyy HH:mm:ss")

        put("loginMobile",loginMobile)
        val randomIndex = Random().nextInt(4)
        if (randomIndex == 1){
            apply {
                put("startTime",launchTime)
            }
            return this
        }
        if (randomIndex == 2){
            apply {
                put("startTime",launchTime)
                put("loginTime",loginTime)
            }
            return this
        }

        if (randomIndex == 3){
            apply {
                put("startTime",launchTime)
                put("loginTime",loginTime)
                put("currentTime",currentTime)
            }
        }
        return this
    }

    /**
     * 获取随机Url
     */
    fun getRandomUrl(index: Int = -1): String{
        var urlIndex = index
        if (urlIndex == -1 || urlIndex > GOMBECREDIT_COMMON_URLS.size - 1){
            urlIndex = Random().nextInt(GOMBECREDIT_COMMON_URLS.size)
        }
        return GOMBECREDIT_COMMON_URLS[urlIndex]
    }
}